home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / osrc.arc / TCP.H < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-05  |  7.8 KB  |  241 lines

  1. /* TCP implementation. Follows RFC 793 as closely as possible */
  2. #ifndef    NULLTCB
  3.  
  4. #include "global.h"
  5. #include "netuser.h"
  6. #include "timer.h"
  7.  
  8. #define    DEF_WND    2048    /* Default receiver window */
  9. #define    NTCB    19    /* # TCB hash table headers */
  10. #define    RTTCACHE 16    /* # of TCP round-trip-time cache entries */
  11. #define    DEF_MSS    512    /* Default maximum segment size */
  12. #define    DEF_RTT    5000    /* Initial guess at round trip time (5 sec) */
  13. #define    MSL2    30    /* Guess at two maximum-segment lifetimes */
  14.  
  15. extern int32 Clock;
  16. #define    geniss()    ((int32)Clock << 18)    /* Increment clock at 4.5 Mbytes/sec */
  17.  
  18. /* Round trip timing parameters */
  19. #define    AGAIN    8    /* Average RTT gain = 1/8 */
  20. #define    LAGAIN    3    /* Log2(AGAIN) */
  21. #define    DGAIN    4    /* Mean deviation gain = 1/4 */
  22. #define    LDGAIN    2    /* log2(DGAIN) */
  23.  
  24. /* TCP segment header -- internal representation
  25.  * Note that this structure is NOT the actual header as it appears on the
  26.  * network (in particular, the offset and checksum fields are missing).
  27.  * All that knowledge is in the functions ntohtcp() and htontcp() in tcpsubr.c
  28.  */
  29. struct tcp {
  30.     int16 source;    /* Source port */
  31.     int16 dest;    /* Destination port */
  32.     int32 seq;    /* Sequence number */
  33.     int32 ack;    /* Acknowledgment number */
  34.     struct {
  35.         char urg;
  36.         char ack;
  37.         char psh;
  38.         char rst;
  39.         char syn;
  40.         char fin;
  41.     } flags;
  42.     int16 wnd;    /* Receiver flow control window */
  43.     int16 up;    /* Urgent pointer */
  44.     int16 mss;    /* Optional max seg size */
  45. };
  46. /* TCP options */
  47. #define    EOL_KIND    0
  48. #define    NOOP_KIND    1
  49. #define    MSS_KIND    2
  50.  
  51. #define    TCPLEN        20
  52. #define    MSS_LENGTH    4
  53. /* Resequencing queue entry */
  54. struct reseq {
  55.     struct reseq *next;    /* Linked-list pointer */
  56.     struct tcp seg;        /* TCP header */
  57.     struct mbuf *bp;    /* data */
  58.     int16 length;        /* data length */
  59.     char tos;        /* Type of service */
  60. };
  61. #define    NULLRESEQ    (struct reseq *)0
  62.  
  63. /* TCP connection control block */
  64. struct tcb {
  65.     struct tcb *prev;    /* Linked list pointers for hash table */
  66.     struct tcb *next;
  67.  
  68.     struct connection conn;
  69.  
  70.     char state;    /* Connection state */
  71. #define    CLOSED        0    /* Must be 0 */
  72. #define    LISTEN        1
  73. #define    SYN_SENT    2
  74. #define    SYN_RECEIVED    3
  75. #define    ESTABLISHED    4
  76. #define    FINWAIT1    5
  77. #define    FINWAIT2    6
  78. #define    CLOSE_WAIT    7
  79. #define    CLOSING        8
  80. #define    LAST_ACK    9
  81. #define    TIME_WAIT    10
  82.  
  83.     char reason;        /* Reason for closing */
  84. #define    NORMAL        0    /* Normal close */
  85. #define    RESET        1    /* Reset by other end */
  86. #define    TIMEOUT        2    /* Excessive retransmissions */
  87. #define    NETWORK        3    /* Network problem (ICMP message) */
  88.  
  89. /* If reason == NETWORK, the ICMP type and code values are stored here */
  90.     char type;
  91.     char code;
  92.  
  93.     /* Send sequence variables */
  94.     struct {
  95.         int32 una;    /* First unacknowledged sequence number */
  96.         int32 nxt;    /* Next sequence num to be sent for the first time */
  97.         int32 ptr;    /* Working transmission pointer */
  98.         int32 wl1;    /* Sequence number used for last window update */
  99.         int32 wl2;    /* Ack number used for last window update */
  100.         int16 wnd;    /* Other end's offered receive window */
  101.         int16 up;    /* Send urgent pointer */
  102.     } snd;
  103.     int32 iss;        /* Initial send sequence number */
  104.     int32 resent;        /* Count of bytes retransmitted */
  105.     int16 cwind;        /* Congestion window */
  106.     int16 ssthresh;        /* Slow-start threshold */
  107.  
  108.     /* Receive sequence variables */
  109.     struct {
  110.         int32 nxt;    /* Incoming sequence number expected next */
  111.         int16 wnd;    /* Our offered receive window */
  112.         int16 up;    /* Receive urgent pointer */
  113.     } rcv;
  114.     int32 irs;        /* Initial receive sequence number */
  115.     int32 rerecv;        /* Count of duplicate bytes received */
  116.     int16 mss;        /* Maximum segment size */
  117.  
  118.     int16 window;        /* Receiver window and send queue limit */
  119.  
  120.     void (*r_upcall)();    /* Call when "significant" amount of data arrives */
  121.     void (*t_upcall)();    /* Call when ok to send more data */
  122.     void (*s_upcall)();    /* Call when connection state changes */
  123.     struct {        /* Control flags */
  124.         char force;    /* We owe the other end an ACK or window update */
  125.         char clone;    /* Server-type TCB, cloned on incoming SYN */
  126.         char retran;    /* A retransmission has occurred */
  127.         char active;    /* TCB created with an active open */
  128.         char synack;    /* Our SYN has been acked */
  129.         char rtt_run;    /* We're timing a segment */
  130.     } flags;
  131.     char tos;        /* Type of service (for IP) */
  132.     char backoff;        /* Backoff interval */
  133.  
  134.     struct mbuf *rcvq;    /* Receive queue */
  135.     struct mbuf *sndq;    /* Send queue */
  136.     int16 rcvcnt;        /* Count of items on rcvq */
  137.     int16 sndcnt;        /* Number of unacknowledged sequence numbers on
  138.                  * sndq. NB: includes SYN and FIN, which don't
  139.                  * actually appear on sndq!
  140.                  */
  141.  
  142.     struct reseq *reseq;    /* Out-of-order segment queue */
  143.     struct timer timer;    /* Retransmission timer */
  144.     int32 rtt_time;        /* Stored clock values for RTT */
  145.     int32 rttseq;        /* Sequence number being timed */
  146.     int32 srtt;        /* Smoothed round trip time, milliseconds */
  147.     int32 mdev;        /* Mean deviation, milliseconds */
  148.  
  149.     int user;        /* User parameter (e.g., for mapping to an
  150.                  * application control block
  151.                  */
  152. };
  153. #define    NULLTCB    (struct tcb *)0
  154. /* TCP round-trip time cache */
  155. struct tcp_rtt {
  156.     int32 addr;        /* Destination IP address */
  157.     int32 srtt;        /* Most recent SRTT */
  158.     int32 mdev;        /* Most recent mean deviation */
  159. };
  160. #define    NULLRTT    (struct tcp_rtt *)0
  161. extern struct tcp_rtt Tcp_rtt[];
  162.  
  163. /* TCP statistics counters */
  164. struct tcp_stat {
  165.     int16 runt;        /* Smaller than minimum size */
  166.     int16 checksum;        /* TCP header checksum errors */
  167.     int16 conout;        /* Outgoing connection attempts */
  168.     int16 conin;        /* Incoming connection attempts */
  169.     int16 resets;        /* Resets generated */
  170.     int16 bdcsts;        /* Bogus broadcast packets */
  171. };
  172. extern struct tcp_stat Tcp_stat;
  173. extern struct tcb *Tcbs[];
  174. extern int16 Tcp_mss;
  175. extern int16 Tcp_window;
  176. extern int32 Tcp_irtt;
  177.  
  178. #if    defined(__STDC__) || defined(__TURBOC__)
  179. void tcp_input(struct mbuf *bp,char protocol,int32 source,int32 dest,
  180.     char tos,int16 length,int rxbroadcast);
  181. void tcp_icmp(int32 source,int32 dest,char type,char code,struct mbuf **bpp);
  182. struct tcb *lookup_tcb(struct connection *conn);
  183. struct tcb *create_tcb(struct connection *conn);
  184. void tcp_output(struct tcb *tcb);
  185. void close_self(struct tcb *tcb,int reason);
  186. void setstate(struct tcb *tcb,int newstate);
  187. struct mbuf *htontcp(struct tcp *tcph,struct mbuf *data,
  188.     struct pseudo_header *ph);
  189. int ntohtcp(struct tcp *tcph,struct mbuf **bpp);
  190. int seq_within(int32 x,int32 low,int32 high);
  191. int seq_lt(int32 x,int32 y);
  192. int seq_le(int32 x,int32 y);
  193. int seq_gt(int32 x,int32 y);
  194. int seq_ge(int32 x,int32 y);
  195. void link_tcb(struct tcb *tcb);
  196. void unlink_tcb(struct tcb *tcb);
  197. void send_syn(struct tcb *tcb);
  198. /* TCP primitives */
  199. struct tcb *open_tcp(struct socket *lsocket,struct socket *fsocket,
  200.     int mode,int16 window,void (*r_upcall)(),void (*t_upcall)(),
  201.     void (*s_upcall)(),int tos,int user);
  202. int send_tcp(struct tcb *tcb,struct mbuf *bp);
  203. int recv_tcp(struct tcb *tcb,struct mbuf **bpp,int16 cnt);
  204. int close_tcp(struct tcb *tcb);
  205. int del_tcp(struct tcb *tcb);
  206. int tcpval(struct tcb *tcb);
  207. int kick_tcp(struct tcb *tcb);
  208. void st_tcp(struct tcb *tcb);
  209. void tcp_dump(struct mbuf **bpp,int32 source,int32 dest,int check);
  210. void reset_tcp(struct tcb *tcb);
  211. char *tcp_port(int16 n);
  212. void rtt_add(int32 addr,int32 rtt);
  213. struct tcp_rtt *rtt_get(int32 addr);
  214. void reset_all(void);
  215.  
  216. #else
  217.  
  218. struct tcb *lookup_tcb();
  219. struct tcb *create_tcb();
  220. void tcp_output(),tcp_input(),close_self(),setstate();
  221. struct mbuf *htontcp();
  222. int ntohtcp();
  223. int seq_within(),seq_lt(),seq_le(),seq_gt(),seq_ge();
  224. void link_tcb(),unlink_tcb();
  225. void tcp_icmp();
  226. void send_syn();
  227. /* TCP primitives */
  228. struct tcb *open_tcp();
  229. int send_tcp(),recv_tcp(),close_tcp(),del_tcp();
  230. int tcpval();
  231. void st_tcp(),tcp_dump();
  232. int kick_tcp();
  233. void reset_tcp();
  234. char *tcp_port();
  235. void rtt_add();
  236. struct tcp_rtt *rtt_get();
  237. void reset_all();
  238. #endif
  239.  
  240. #endif    /* NULLTCB */
  241.